package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //1.获取登录用户ID,并存入shoppingCart属性上
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //2.创建条件构造器
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //2.1 添加条件
        lqw.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        //3.根据条件查询
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
        //判断库中是否有该商品
        if (cartServiceOne != null) {
            //获取该商品的number
            Integer number = cartServiceOne.getNumber();
            //设置值
            cartServiceOne.setNumber(number + 1);
            //修改
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //没有该商品进行添加
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //获取登录用户的ID,
        Long userId = BaseContext.getCurrentId();
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //添加用户ID条件
        lqw.eq(ShoppingCart::getUserId, userId);
        //添加排序条件
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        //返回结果
        return R.success(list);
    }

    /**
     * 移除购物车商品数量
     *
     * @param shoppingCart
     * @return
     */
    @PutMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        //1.获取用户ID存入shoppingCart属性中
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //2. 创建条件构造器
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //2.1 添加用户id条件
        lqw.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        //2.2 添加套餐条件
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        //2.3 添加菜品条件
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //2.4 查询该条件下的数据
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);

        //3. 获取该数据下的number并减一,判断number的值进行不同操作
        Integer number = cartServiceOne.getNumber() - 1;

        //3.1判断number的值
        if (number != 0) {
            //如果不等于零对商品的number进行修改
            cartServiceOne.setNumber(number);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //删除该商品
            shoppingCartService.removeById(cartServiceOne.getId());
        }
        return R.success("购物车商品移除成功");
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //1.获取登录用户的id
        Long userId = BaseContext.getCurrentId();
        //2.创建条件构造器
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(lqw);
        return R.success("购物车清除成功");
    }
}
